package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.queryparam.NursingProjectQueryParam;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getPage(NursingProjectQueryParam npqp) {
        //设置分页参数
        PageHelper.startPage(npqp.getPageNum(), npqp.getPageSize());

        //查询数据
        List<NursingProject> nps = nursingProjectMapper.getPage(npqp);

        for (NursingProject np : nps) {
            //获取项目id
            Long npId = np.getId();

            //根据id查询是否有关联护理计划
            List<NursingProjectPlan> counts = nursingProjectMapper.getCount(npId);

            //根据集合的长度可以知道是否有关联护理计划 （0=没有）
            np.setCount(counts.size());
        }

        //封装给Page
        Page<NursingProject> page = (Page<NursingProject>) nps;

        //数据封装
        return PageResponse.of(page, NursingProjectVo.class);
    }

    @Override
    public void addNursingProject(NursingProjectDto npd) {
        //把DTO对象 转换成 实体类对象
        NursingProject nursingProject = BeanUtil.toBean(npd, NursingProject.class);

        //调用mapper 保存数据
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    @Override
    public NursingProjectVo getByIdNursingProject(Long id) {
        NursingProject np = nursingProjectMapper.getByIdNursingProject(id);

        //获取项目id
        Long npId = np.getId();

        //根据id查询是否有关联护理计划
        List<NursingProjectPlan> counts = nursingProjectMapper.getCount(npId);

        //根据集合的长度可以知道是否有关联护理计划 （0=没有）
        np.setCount(counts.size());

        //把实体类对象的值 赋给 VO对象
        NursingProjectVo npv = new NursingProjectVo();
        BeanUtils.copyProperties(np, npv);

        return npv;
    }

    @Override
    public void updateNursingProject(NursingProjectDto npd) {
        NursingProject nursingProject = BeanUtil.toBean(npd, NursingProject.class);

        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    @Override
    public void statusNursingProject(Long id, Integer status) {
        nursingProjectMapper.statusNursingProject(id, status);
    }

    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);
    }

    @Override
    public List<NursingProjectVo> allList() {
        //查询所有的护理项目
        List<NursingProject> npList = nursingProjectMapper.allList();

        for (NursingProject np : npList) {
            //获取项目id
            Long npId = np.getId();

            //根据id查询是否有关联护理计划
            List<NursingProjectPlan> counts = nursingProjectMapper.getCount(npId);

            //根据集合的长度可以知道是否有关联护理计划 （0=没有）
            np.setCount(counts.size());
        }

        //VO对象集合
        List<NursingProjectVo> npvList = new ArrayList<>();

        //把实体类对象 转换成 VO对象 存入VO对象集合
        for (NursingProject np : npList) {
            NursingProjectVo npv = BeanUtil.toBean(np, NursingProjectVo.class);
            npvList.add(npv);
        }

        //返回VO对象集合
        return npvList;
    }
}
